package com.graphsql.gamesdk.base;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.Toast;

import com.graphsql.gamesdk.util.ResourceUtil;
import com.graphsql.gamesdk.util.ToastUtil;

public abstract class BaseDialog extends Dialog {

    protected LayoutInflater factory;

    private ProgressDialog mProgressDialog;

    public static final int SHOW_PROGRESS = 0;

    public static final int REMOVE_PROGRESS = 1;

    protected boolean processingFlag = false;

    private Handler mHandler = new Handler(Looper.getMainLooper()) {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_PROGRESS:
                    // if hide, isShowing() will return true, bug
                    // if(mProgressDialog.isShowing() == false)
                	try{
	                    mProgressDialog.show();
	                    mProgressDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
	                        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
	                            if (keyCode == KeyEvent.KEYCODE_SEARCH) {
	                                return true;
	                            }
	                            return false;
	                        }
	                    });
                	}catch (Exception e) {
                		e.printStackTrace();
					}
                    break;
                case REMOVE_PROGRESS:
                	try{
	                    processingFlag = false;
	                    mHandler.removeMessages(SHOW_PROGRESS);
	                    mProgressDialog.hide();
                	}catch (Exception e) {
                		e.printStackTrace();
					}
                    break;
                default:
    				onHandleMessage(msg);
    				break;
            }
        }
    };
    protected void onHandleMessage(Message msg) {
	}

    protected final void sendMessage(int what) {
        mHandler.sendMessage(mHandler.obtainMessage(what));
    }
    
    protected final void sendMessage(int what, Object obj) {
		mHandler.sendMessage(mHandler.obtainMessage(what, obj));
	}

    protected final void sendMessageDelayed(int what, long delayMillis) {
        mHandler.sendMessageDelayed(mHandler.obtainMessage(what), delayMillis);
    }

    protected BaseDialog(Context context, int theme) {
        super(context, theme);
        factory = LayoutInflater.from(context);

        mProgressDialog = new ProgressDialog(context); // 进度是否是不确定的，这只和创建进度条有关
        mProgressDialog.setMessage(context
                .getString(ResourceUtil.getStringId(context, "gs_string_wait")));
        mProgressDialog.setCancelable(false);
    }
//    protected void initWH(Context context){
//        //set window params
//        Window window = getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//        //set width,height by density and gravity
//        Resources resources = context.getResources();
//        DisplayMetrics dm = resources.getDisplayMetrics();
//        float density = dm.density;
//        params.width = (int) (280*density);
//        params.height = (int) (260*density);
//        params.gravity = Gravity.CENTER;
//        window.setAttributes(params);
//    }
//    protected void initWarpWH(Context context){
//        //set window params
//        Window window = getWindow();
//        WindowManager.LayoutParams params = window.getAttributes();
//        //set width,height by density and gravity
//        Resources resources = context.getResources();
//        DisplayMetrics dm = resources.getDisplayMetrics();
//        float density = dm.density;
//        params.width = LayoutParams.WRAP_CONTENT;
//        params.height = LayoutParams.WRAP_CONTENT;
//        params.gravity = Gravity.CENTER;
//        window.setAttributes(params);
//    }

    protected void onNetworkError() {
        ToastUtil.showMessage(this.getContext(), ResourceUtil.getStringId(this.getContext(), "gs_string_networkerrorinfo"),
                Toast.LENGTH_LONG);
    }

    @Override
    protected void onStop() {
    	super.onStop();
        try {
			mProgressDialog.dismiss();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }


}
